package cn.shadow.module.scene;

import lombok.Data;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 视野格子
 */
@Data
public class ViewGrid {

    /**
     * 场景id
     */
    private int viewId;

    /**
     * 坐标索引值
     */
    public int xIdx;
    /**
     * 坐标索引值
     */
    public int yIdx;

    /**
     * 中心点
     */
    private int centerX;
    /**
     * 中心点
     */
    private int centerY;

    /**
     * 格子大小
     */
    private int viewGridLength;
    /**
     * 这个格子里面的所有场景对象,包括凋落物、玩家、怪物
     */
    private ConcurrentHashMap<Long, SceneObj> objs = new ConcurrentHashMap<>();


    /**
     * 所有对这个格子感兴趣的角色
     */
    private Map<Long, HeroViewObj> interestHero = new ConcurrentHashMap<>();

    /**
     * 排序后的兴趣列表
     */
    private List<HeroViewObj> sortedInterestHero;

    private boolean isSeeAll;

    public ViewGrid(int xIdx, int yIdx, boolean isSeeAll, int viewGridLength) {
        this.xIdx = xIdx;
        this.yIdx = yIdx;
        this.isSeeAll = isSeeAll;
        this.viewGridLength = viewGridLength;
        this.centerX = xIdx * viewGridLength + (viewGridLength >> 1);
        this.centerY = yIdx * viewGridLength + (viewGridLength >> 1);
    }

    /**
     * 判断是否已经在兴趣列表里面
     */
    public boolean isInInterest(long id) {
        return interestHero.containsKey(id);
    }

    /**
     * 将这个玩家移除出去
     */
    public void removeObj(long id) {
        SceneObj sceneObj = objs.remove(id);
        if (sceneObj == null) return;
        //判断obj的类型，然后进行相应的操作
    }

    public void addObj(SceneObj sceneObj) {
        objs.putIfAbsent(sceneObj.getId(), sceneObj);
        //判断sceneObj什么类型，然后进行后续操作
    }

    /**
     * @param heroViewObj 新加入的玩家
     * @param ctime
     */
    public void onInterest(HeroViewObj heroViewObj, long ctime) {
        if (isInInterest(heroViewObj.getId())) {
            return;
        }
        interestHero.putIfAbsent(heroViewObj.getId(),heroViewObj);
        System.out.println(String.format("玩家【%s】对格子【(%s,%s)】感兴趣,格子兴趣列表:%s", heroViewObj.getId(), this.xIdx, this.yIdx, interestHero.size()));
        //遍历所有场景对象
        for (Iterator<SceneObj> itr = objs.values().iterator(); itr.hasNext(); ) {
            SceneObj sceneObj = itr.next();
            if (sceneObj.getId() == heroViewObj.getId()) {
                continue;
            }
            if (!sceneObj.getViewObj().isHeroCanSeeMe(heroViewObj)) {//看不到
                continue;
            }
            sceneObj.doSendMeToHero(heroViewObj, ctime, false); //把格子所有对象消息发给新加入的玩家
        }
    }

    public void unInterest(HeroViewObj viewObj) {
        interestHero.remove(viewObj.getId());
        System.out.println(String.format("格子移除玩家【%s】兴趣列表:%s", viewObj.getId(), interestHero.size()));
        for (SceneObj obj : objs.values()) {
            if (obj.getId() == viewObj.getId()) {
                continue;
            }
            ViewObj objViewObj = obj.getViewObj();
            if (!objViewObj.isHeroCanSeeMe(viewObj)) { //
                continue;
            }
            objViewObj.doRemoveFromHero(viewObj); //告诉我，其他人我都不想见了
        }
    }
}
